package com.cloud.bc.controller;

import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.model.entity.BcUserBankcards;
import com.cloud.bc.service.BcFinancesService;
import com.cloud.bc.service.BcUserBankcardsService;
import com.cloud.bc.service.BcUsersService;
import com.cloud.common.model.enums.WithdrawalBindTypeEnum;
import com.cloud.wx.config.WeiXinPayConfig;
import com.cloud.wx.model.enums.WxConst;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

/**
 * 提现
 */
@AllArgsConstructor
@Controller
@RequestMapping("m/center/account/withdrawal")
public class MCenterAccountWithdrawalController extends BaseController {
    private final WeiXinPayConfig weiXinPayConfig;
    private final BcUsersService bcUsersService;
    private final BcUserBankcardsService bcUserBankcardsService;
    private final BcFinancesService bcFinancesService;

    /**
     * 提现
     */
    @GetMapping("")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "提现", "m/center/account/withdrawal/index");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUserBankcards bcUserBankcards = new BcUserBankcards();
        bcUserBankcards.setUserId(userInfo.getId());
        List<BcUserBankcards> bcUserBankcardsList = bcUserBankcardsService.selectAll(bcUserBankcards);
        boolean isBindWeixin = bcUserBankcardsList.stream().filter(t -> t.getWay().equals(WithdrawalBindTypeEnum.WEIXINPAY.getName())).count() > 0 ? true : false;
//        boolean isBindAlipay=bcUserBankcardsList.stream().filter(t->t.getWay().equals(WithdrawalBindTypeEnum.ALIPAY.getName())).count()>0?true:false;
        BcFinances bcFinances = bcFinancesService.selectOne(new BcFinances().setUserId(userInfo.getId()));
        BigDecimal money = BigDecimal.ZERO;
        if (null != bcFinances) {
            money = bcFinances.getMoney();
        }

        view.addObject("isBindWeixin", isBindWeixin);
        view.addObject("money", money);
        return view;
    }

    /**
     * 提现方式
     */
    @GetMapping("way")
    public ModelAndView way(HttpServletRequest request, HttpServletResponse response) {
        ModelAndView view = baseView(request, "提现方式", "m/center/account/withdrawal/way");
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcUserBankcards bcUserBankcards = new BcUserBankcards();
        bcUserBankcards.setUserId(userInfo.getId());
        List<BcUserBankcards> bcUserBankcardsList = bcUserBankcardsService.selectAll(bcUserBankcards);
        boolean isBindWeixin = bcUserBankcardsList.stream().filter(t -> t.getWay().equals(WithdrawalBindTypeEnum.WEIXINPAY.getName())).count() > 0 ? true : false;
//        boolean isBindAlipay=bcUserBankcardsList.stream().filter(t->t.getWay().equals(WithdrawalBindTypeEnum.ALIPAY.getName())).count()>0?true:false;
        view.addObject("isBindWeixin", isBindWeixin);
        return view;
    }

    /**
     * 提现方式
     */
    @GetMapping("wxpay/edit/redirect")
    public ModelAndView wxpayEditRedirect(HttpServletRequest request, HttpServletResponse response) {
        String url = WxConst.wxOAuthUrl;
        String notifyUrl = weiXinPayConfig.getNotifyUrl();
        notifyUrl = notifyUrl + "wx/withdrawalBind";
        url = url.replace("{appid}", config.getWxappid()).replace("{redirect_uri}", notifyUrl);
        return new ModelAndView("redirect:" + url);
    }

    /**
     * 提现方式
     */
    @GetMapping("wxpay/edit")
    public ModelAndView wxpayEdit(HttpServletRequest request, HttpServletResponse response) {
        String openid = request.getParameter("openid");
        ModelAndView view = baseView(request, "提现方式", "m/center/account/withdrawal/wxpay_edit");
        UserInfo userInfo = bcUsersService.getUserInfo(request);

        BcUserBankcards bcUserBankcards = new BcUserBankcards();
        bcUserBankcards.setUserId(userInfo.getId());
        bcUserBankcards.setWay(WithdrawalBindTypeEnum.WEIXINPAY.getName());

        BcUserBankcards bcUserBankcardsResult = bcUserBankcardsService.selectOne(bcUserBankcards);
        String trueName = "";
        if (null != bcUserBankcardsResult) {
            trueName = bcUserBankcardsResult.getName();
        }
        view.addObject("openid", openid);
        view.addObject("trueName", trueName);
        return view;
    }
}
